package rudy.elevator.ui;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;

import javax.swing.*;

import rudy.elevator.bean.*;
import rudy.elevator.bussiness.*;
import rudy.elevator.thread.*;

public class MainPanel extends JPanel implements KeyListener{
    private LiftBean [] liftbean;
 //   private LiftFlower [] liftflower;
    private static int LIFTNUMBER =2;
    public static int maxFloor = 15;
    private static int WIDTH = 300;
    private static int HEIGHT = 600;
    private static int LIFTWIDTH = 80;
    public static int minFloor=5;
    private int currentliftwidth =80;
    private int  averagewidth;
    private int  averageheight;
    CloseDoor   closedoorthread;
    OpenDoor   opendoorthread;
    UpLift  uplift;
    DownLift downlift;
    private boolean  initcontrol = false;
    private  ArrayList controllist = new ArrayList();
    private static int BUTTONWIDTH =10;
    private RudyEllipse2D current;

    private int xx =0;
    private int yy =0;
    private MainControl  maincontrol;
    private MainPanel   panel;
    private Image   imageup;
    private Image   imagedown;
    private FlowerControl flowercontrol;
    public MainPanel(int min,int max) {
    	this.minFloor=min;
    	this.maxFloor=max;
       // super.paintComponent(Graphics g);
       init();
       addKeyListener(this);
       setFocusable(true);
       addMouseListener(new MouseHandler());
       addMouseMotionListener(new MouseMotionHandler());
       panel = this;
    }

    public void init()
    {
        liftbean = new LiftBean[LIFTNUMBER-1];
        for(int i=0;i<liftbean.length;i++)
        {
            liftbean[i] =new LiftBean();
            liftbean[i].setLiftid(i);
             LiftFlower [] liftflower  = new LiftFlower[maxFloor-minFloor];
            for (int j = 0; j < maxFloor-minFloor; j++) {
                liftflower[j] = new LiftFlower();
                if (j == 0) {
                    liftflower[j].setState(true);
                }
                liftflower[j].setNumber(j);
            }
            liftbean[i].setFlowers(liftflower);
        }




        initControlButton();
        initImage();
        initControl();
        for (int i = 0; i < liftbean.length; i++) {
            MainControl liftcontrol = new MainControl(this, liftbean[i]);
                     liftcontrol.start();
        }


      //  maincontrol = new MainControl(this);
    }


    public void initControl()
    {
         flowercontrol  = new FlowerControl();
         FlowerControlBean   controlbean [] = new FlowerControlBean[maxFloor-minFloor];
         for(int i=0;i<(maxFloor-minFloor);i++)
         {
             controlbean[i] = new FlowerControlBean();
             controlbean[i].setFlowerid(i);
         }
         flowercontrol.setControlbean(controlbean);
    }

    public void initImage() {
         imageup = Toolkit.getDefaultToolkit().getImage(this.getClass().
                getClassLoader().getResource("rudy/image/imageup.gif"));
      //  imageup = new ImageIcon(image);
        imagedown = Toolkit.getDefaultToolkit().getImage(this.getClass().
                getClassLoader().getResource("rudy/image/imagedown.gif"));
      //  imagedown = new ImageIcon(image);
    }




    public void paintComponent(Graphics g)
    {
//        if(this.initcontrol == false)
//        {
//             initControlButton();
//             this.initcontrol = true;
//        }
        super.paintComponent(g);
        Graphics2D   g2 = (Graphics2D) g;
        initpaint(g2);
        drawLift(g2);
    }

    public void initpaint(Graphics2D  g2)
    {
        averagewidth = this.WIDTH/LIFTNUMBER;
        averageheight = (this.HEIGHT-100)/(maxFloor-minFloor+1);
        for(int i=0;i<LIFTNUMBER-1;i++)
        {
            if( LIFTWIDTH > averagewidth)
                this.currentliftwidth = averagewidth;
             int m_point   =(averagewidth -currentliftwidth)/2;
             int x_point1 = averagewidth*i + m_point;
             int x_point2 = x_point1+currentliftwidth;
             int mm_point   =(averagewidth -100)/2;
            for(int j=0;j<maxFloor-minFloor +1;j++)
            {
                 int y_point = averageheight*j + mm_point;
                 g2.drawLine(x_point1,y_point,x_point2,y_point);
                 if(j ==0)
                 {
                      String str  =""  +  (this.liftbean[i].getCurrentlyflower()+minFloor);
                      g2.setColor(Color.red);
                      g2.drawString(str,x_point1+(x_point2-x_point1)/2,y_point);
                      g2.setColor(Color.BLACK);
                      if(this.liftbean[i].isIsup())
                      {
                          g2.drawImage(imageup,x_point1+(x_point2-x_point1)/2 -9,y_point-9,null);
                      }else if(this.liftbean[i].isIsdown())
                      {
                           g2.drawImage(imagedown,x_point1+(x_point2-x_point1)/2 -9,y_point-9,null);
                      }

                 }
                 if((LIFTNUMBER ==1 || i<LIFTNUMBER-1) && j <(maxFloor-minFloor))
                 {
                  // g2.fill(new RudyEllipse2D(x_point2+m_point -BUTTONWIDTH,y_point+averageheight/2-BUTTONWIDTH,BUTTONWIDTH,BUTTONWIDTH));
                  // g2.fill(new RudyEllipse2D(x_point2+m_point -BUTTONWIDTH,y_point+averageheight/2+BUTTONWIDTH,BUTTONWIDTH,BUTTONWIDTH));
                   RudyEllipse2D   el =getRudyEllipse2D(i,maxFloor-minFloor-j,1);
                   if(el != null)
                   {
                       if(el.isIspress())
                       {
                            g2.setColor(Color.red);
                            g2.fill(el);
                       }else
                       {
                           g2.fill(el);
                       }
                       g2.setColor(Color.BLACK);
                   }
                   el =getRudyEllipse2D(i,maxFloor-minFloor-j,2);
                   if( el != null)
                   {
                       if (el.isIspress()) {
                           g2.setColor(Color.red);
                           g2.fill(el);
                       } else {

                           g2.fill(el);
                       }
                       g2.setColor(Color.BLACK);
                   }
                 }
            }
            int y_point1 = averageheight*(maxFloor-minFloor) + mm_point;
            g2.drawLine(x_point1,mm_point,x_point1,y_point1);
            g2.drawLine(x_point2,mm_point,x_point2,y_point1);
            if(liftbean[i].isInit() ==false )
            {
                liftbean[i].setX_point1(x_point1);
                liftbean[i].setX_point2(x_point2);
                liftbean[i].setY_point1(y_point1);
                liftbean[i].setY_point2(y_point1-averageheight);

                liftbean[i].setFlowerhight(averageheight);
                liftbean[i].setInit(true);
            }


        }
      //  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      //  Dimension panelSize = this.getSize();
    }


    public void initControlButton()
   {
       averagewidth = this.WIDTH/(LIFTNUMBER);
       averageheight = (this.HEIGHT-100)/(maxFloor-minFloor+1);
       for(int i=0;i<LIFTNUMBER;i++)
       {
           if( LIFTWIDTH > averagewidth)
               this.currentliftwidth = averagewidth;
            int m_point   =(averagewidth -currentliftwidth)/2;
            int x_point1 = averagewidth*i + m_point;
            int x_point2 = x_point1+currentliftwidth;
            int mm_point   =(averagewidth -100)/2;

           for(int j=0;j<this.maxFloor-minFloor;j++)
           {
               int y_point = averageheight*j + mm_point;
               RudyEllipse2D  el=   new RudyEllipse2D(x_point2+m_point -BUTTONWIDTH,y_point+averageheight/2-BUTTONWIDTH,BUTTONWIDTH,BUTTONWIDTH);
               el.setLiftid(i);
               el.setFlowerid(this.maxFloor-minFloor -j);
               el.setFlag(1);
               el.setIspress(false);
               this.controllist.add(el);
/*
               RudyEllipse2D  el2=   new RudyEllipse2D(x_point2+m_point -BUTTONWIDTH,y_point+averageheight/2+BUTTONWIDTH,BUTTONWIDTH,BUTTONWIDTH);

               el2.setLiftid(i);
               el2.setFlowerid(this.maxFloor-minFloor - j);
               el2.setFlag(2);
               el2.setIspress(false);
               this.controllist.add(el2);
*/
           //     g2.drawLine(x_point1,y_point,x_point2,y_point);

           }
       //    int y_point1 = averageheight*(this.FLOWERLEVER) + mm_point;
//           g2.drawLine(x_point1,mm_point,x_point1,y_point1);
//           g2.drawLine(x_point2,mm_point,x_point2,y_point1);


       }
     //  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
     //  Dimension panelSize = this.getSize();
   }

   public RudyEllipse2D getRudyEllipse2D(int liftid,int flowerid,int flag)
   {
       if( controllist != null)
       {
          for(int i= 0;i<controllist.size();i++)
          {
              RudyEllipse2D   el = (RudyEllipse2D)controllist.get(i);
             if( el.getLiftid() == liftid && el.getFlowerid()==flowerid && el.getFlag() == flag)
             {
                 return el;
             }
          }
       }
       return null;
   }


   public void setControlButton(int flowerid,int  flag)
   {
         for (int i = 0; i < controllist.size(); i++) {
           RudyEllipse2D el2 = (RudyEllipse2D) controllist.get(i);
           if( el2.getFlowerid() == flowerid && el2.getFlag() == flag)
           {
                el2.setIspress(false);
           }
         }
   }

   public void setControlbutton(RudyEllipse2D  el)
   {
       boolean bool = el.isIspress();
       for (int i = 0; i < controllist.size(); i++) {
           RudyEllipse2D el2 = (RudyEllipse2D) controllist.get(i);
           if (el.getFlowerid() == el2.getFlowerid() &&
               el.getFlag() == el2.getFlag()) {
               if (bool) {
                   el2.setIspress(false);
               } else {
                   el2.setIspress(true);
               }
           }
       }
       FlowerControlBean flowercbean = this.flowercontrol.getControlbean()[el.
                                       getFlowerid()-1];
       if(el.getFlag() ==1)
       {
           if(bool)
           {
               flowercbean.setIsUpPress(false);
           }else
           {
                flowercbean.setIsUpPress(true);
           }
       }else
       {
           if(bool)
           {
               flowercbean.setIsDownPress(false);
           }else
           {
               flowercbean.setIsDownPress(true);
           }
       }


//       if(/*checkAllstop()*/true)
//       {
//          int  ran = randomInt(this.liftbean.length);
//        //  this.getFlowercontrol().setLiftid(ran);
//           flowercbean.setLiftid(ran);
//       }

          int  chooseid = checkSelect(flowercbean,el);
          if(chooseid != -1)
          {
               flowercbean.setLiftid(chooseid);
          }
       System.out.println(flowercbean.isIsUpPress());
       System.out.println(flowercbean.isIsDownPress());

   }



public int  checkSelect(FlowerControlBean fb,RudyEllipse2D el2)
{

    int  selectliftid;
    int flowerid = fb.getFlowerid() -1;
    for(int i=0;i<this.liftbean.length;i++)
    {

           if (liftbean[i].getCurrentlyflower() == flowerid && liftbean[i].isWait())
           {
               return  i;
           }
    }

    if(flagliftsameWapwithWait(flowerid,el2.getFlag()) == true)
    {
        return this.chooseliftid;
    }
    if(flagliftdifferentWap(flowerid,el2.getFlag()) == true)
    {
        return this.chooseliftid;
    }

    return -1;
}


public boolean flagliftdifferentWap(int flowerid,int flag)
{
    boolean hasflag = false;
    int   contrast = -1;
    for (int i = 0; i < this.liftbean.length; i++) {
        if (flag == 1) {
            if (liftbean[i].getCurrentlyflower() < flowerid) {
                if (liftbean[i].isIsdown()) {
                    hasflag = true;
                    if ( contrast ==-1)
                    {
                        contrast = flowerid - liftbean[i].getCurrentlyflower();
                        chooseliftid =i;
                    }
                    else
                    {
                        int temp = flowerid - liftbean[i].getCurrentlyflower();
                        if(temp  < contrast)
                        {
                           chooseliftid =i;
                        }
                    }

                }
            }else
            {
                if(liftbean[i].isIsup())
                {
                    hasflag = true;
                    if ( contrast ==-1)
                    {
                        contrast = liftbean[i].getCurrentlyflower() - flowerid;
                        chooseliftid =i;
                    }
                   else
                   {
                       int temp =  liftbean[i].getCurrentlyflower() -flowerid;
                       if(temp  > contrast)
                       {
                          chooseliftid =i;
                       }
                   }

                }

            }
//                else
//            {
//                if (liftbean[i].isIsdown()|| liftbean[i].isWait()) {
//                     hasflag = true;
//                     if( contrast == -1)
//                     {
//                         if (contrast == -1)
//                             contrast =liftbean[i].getCurrentlyflower() - flowerid;
//                         else {
//                             int temp = liftbean[i].getCurrentlyflower() -flowerid;
//                             if (temp < contrast) {
//                                 chooseliftid = i;
//                             }
//                         }
//
//                     }
//                 }
//
//            }
        } else if (flag == 2) {
            if (liftbean[i].getCurrentlyflower() > flowerid) {
                if (liftbean[i].isIsup()) {
                    hasflag = true;

                        if (contrast == -1)
                        {
                            contrast = liftbean[i].getCurrentlyflower() - flowerid;
                            chooseliftid = i;
                        }
                        else {
                            int temp = liftbean[i].getCurrentlyflower() -flowerid;
                            if (temp > contrast) {
                                chooseliftid = i;
                            }
                        }
                }
            }else
            {
                hasflag = true;
                if (contrast == -1) {
                    contrast =flowerid  -liftbean[i].getCurrentlyflower();
                    chooseliftid = i;
                } else {
                    int temp =flowerid - liftbean[i].getCurrentlyflower();
                    if (temp > contrast) {
                        chooseliftid = i;
                    }
                }

            }
        }
    }


     return hasflag;
}


 public boolean flagliftsameWapwithWait(int flowerid,int flag)
{
    boolean hasflag = false;
    int   contrast = -1;
    for (int i = 0; i < this.liftbean.length; i++) {
        if (flag == 1) {
            if (liftbean[i].getCurrentlyflower() < flowerid) {
                if (liftbean[i].isIsup() ||liftbean[i].isWait()) {
                    hasflag = true;
                    if ( contrast ==-1)
                    {
                        contrast = flowerid - liftbean[i].getCurrentlyflower();
                        chooseliftid = i;
                    }
                    else
                    {
                        int temp = flowerid - liftbean[i].getCurrentlyflower();
                        if(temp  < contrast)
                        {
                           chooseliftid =i;
                        }
                    }

                }
            }
           else
            {
                if (liftbean[i].isWait()) {
                     hasflag = true;
                         if (contrast == -1)
                         {
                             contrast = liftbean[i].getCurrentlyflower() -
                                        flowerid;
                             chooseliftid = i;
                         }
                         else {
                             int temp = liftbean[i].getCurrentlyflower() -flowerid;
                             if (temp < contrast) {
                                 chooseliftid = i;
                             }
                         }
                 }

            }
        } else if (flag == 2) {
            if (liftbean[i].getCurrentlyflower() > flowerid) {
                if (liftbean[i].isIsdown()|| liftbean[i].isWait()) {
                    hasflag = true;

                        if (contrast == -1){
                            contrast = liftbean[i].getCurrentlyflower() - flowerid;
                             chooseliftid = i;
                        }
                        else {
                            int temp = liftbean[i].getCurrentlyflower() -flowerid;
                            if (temp < contrast) {
                                chooseliftid = i;
                            }
                        }
                }
            }else
            {
                if (liftbean[i].isWait()) {
                    hasflag = true;
                    if( contrast == -1)
                    {
                        if (contrast == -1)
                            contrast =flowerid -liftbean[i].getCurrentlyflower();
                        else {
                            int temp =flowerid- liftbean[i].getCurrentlyflower();
                            if (temp < contrast) {
                                chooseliftid = i;
                            }
                        }

                    }
                }

            }
        }
    }


     return hasflag;
}





public int randomInt(int scope) {
     if (scope <= 0) {
         return 0;
     }
     Random rand = new Random();
     int randint = Math.abs(rand.nextInt());
     return randint % scope;
 }

 public boolean  checkAllstop()
 {
    LiftBean  lifts  [] =    panel.getLiftbean();
    boolean  stop = true;
    for(int i=0;i<lifts.length;i++)
    {
        if(!lifts[i].isWait())
        {
          return   stop = false;
        }
    }
   return stop;
 }




    public void drawLift(Graphics2D  g2)
    {

        for(int i=0;i<LIFTNUMBER-1;i++)
        {

            g2.setColor(Color.red);
            g2.drawRect(liftbean[i].getX(),liftbean[i].getY(),liftbean[i].getDx(),liftbean[i].getDy());
            if(liftbean[i].isIsopen())
            {
             //  g2.drawLine(liftbean[i].getX()+liftbean[i].getDx()/2,liftbean[i].getY(),liftbean[i].getX()+liftbean[i].getDx()/2, liftbean[i].getY()+liftbean[i].getDy());
            }else
            {
                g2.drawLine(liftbean[i].getX()+liftbean[i].getDx()/2 -liftbean[i].getXx(),liftbean[i].getY(),liftbean[i].getX()+liftbean[i].getDx()/2-liftbean[i].getXx(), liftbean[i].getY()+liftbean[i].getDy());
                g2.drawLine(liftbean[i].getX()+liftbean[i].getDx()/2+liftbean[i].getXx(),liftbean[i].getY(),liftbean[i].getX()+liftbean[i].getDx()/2+liftbean[i].getXx(), liftbean[i].getY()+liftbean[i].getDy());
            }
        }

    }

    public void keyTyped(KeyEvent e) {
        //  System.out.println("Typped: " + e.getKeyCode());
    }

    public void keyPressed(KeyEvent e) {
        //  System.out.println("keyPressed: " + e.getKeyCode());
    }

    public void keyReleased(KeyEvent e) {
       // e.getKeyCode()
        System.out.println("keyReleased: " + e.getKeyCode());
        if(e.getKeyCode() ==KeyEvent.VK_UP)
        {
          //  System.out.println("$$$$$$$$$$$$$$$$$$$$$");
//                uplift = new UpLift(this);
//                uplift.start();
        }if(e.getKeyCode() == KeyEvent.VK_DOWN)
        {
//              downlift = new DownLift(this);
//               downlift.start();
        }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT)
        {
//            if(closedoorthread == null)
//            {
                closedoorthread = new CloseDoor(this);
//            }
            closedoorthread.start();
          //  closeDoor();
        }
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
//            if(opendoorthread == null)
//            {
                opendoorthread = new OpenDoor(this);
//            }
            opendoorthread.start();
         //   openDoor();
        }
        setKeyRelease(e);

    }

    int keycode =-1;
    public void setKeyRelease(KeyEvent e)
    {
        if(keycode == e.getKeyCode())
        {
            return;
        }
        keycode = e.getKeyCode();

        if (keycode == KeyEvent.VK_0) {
            setliftpress(0,10);
        }
        if (keycode == KeyEvent.VK_1) {
             setliftpress(0,1);
        }
        if (keycode == KeyEvent.VK_2) {
             setliftpress(0,2);
        }
        if (keycode == KeyEvent.VK_3) {
             setliftpress(0,3);
        }
        if (keycode == KeyEvent.VK_4) {
             setliftpress(0,4);
        }
        if (keycode == KeyEvent.VK_5) {
             setliftpress(0,5);
        }
        if (keycode == KeyEvent.VK_6) {
            setliftpress(0,6);
        }
        if (keycode == KeyEvent.VK_7) {
             setliftpress(0,7);
        }
        if (keycode == KeyEvent.VK_8) {
             setliftpress(0,8);
        }
        if (keycode == KeyEvent.VK_9) {
             setliftpress(0,9);
        }

        if (keycode == KeyEvent.VK_P) {
          setliftpress(1,10);
      }
      if (keycode == KeyEvent.VK_Q) {
           setliftpress(1,1);
      }
      if (keycode == KeyEvent.VK_W) {
           setliftpress(1,2);
      }
      if (keycode == KeyEvent.VK_E) {
           setliftpress(1,3);
      }
      if (keycode == KeyEvent.VK_R) {
           setliftpress(1,4);
      }
      if (keycode == KeyEvent.VK_T) {
           setliftpress(1,5);
      }
      if (keycode == KeyEvent.VK_Y) {
          setliftpress(1,6);
      }
      if (keycode == KeyEvent.VK_U) {
           setliftpress(1,7);
      }
      if (keycode == KeyEvent.VK_I) {
           setliftpress(1,8);
      }
      if (keycode == KeyEvent.VK_O) {
           setliftpress(1,9);
      }

      if (keycode == KeyEvent.VK_SEMICOLON) {
  setliftpress(2,10);
}
if (keycode == KeyEvent.VK_A) {
   setliftpress(2,1);
}
if (keycode == KeyEvent.VK_S) {
   setliftpress(2,2);
}
if (keycode == KeyEvent.VK_D) {
   setliftpress(2,3);
}
if (keycode == KeyEvent.VK_F) {
   setliftpress(2,4);
}
if (keycode == KeyEvent.VK_G) {
   setliftpress(2,5);
}
if (keycode == KeyEvent.VK_H) {
  setliftpress(2,6);
}
if (keycode == KeyEvent.VK_J) {
   setliftpress(2,7);
}
if (keycode == KeyEvent.VK_K) {
   setliftpress(2,8);
}
if (keycode == KeyEvent.VK_L) {
   setliftpress(2,9);
}


if (keycode == KeyEvent.VK_SLASH) {
  setliftpress(3,10);
}
if (keycode == KeyEvent.VK_Z) {
   setliftpress(3,1);
}
if (keycode == KeyEvent.VK_X) {
   setliftpress(3,2);
}
if (keycode == KeyEvent.VK_C) {
   setliftpress(3,3);
}
if (keycode == KeyEvent.VK_V) {
   setliftpress(3,4);
}
if (keycode == KeyEvent.VK_B) {
   setliftpress(3,5);
}
if (keycode == KeyEvent.VK_N) {
  setliftpress(3,6);
}
if (keycode == KeyEvent.VK_M) {
   setliftpress(3,7);
}
if (keycode == KeyEvent.VK_COMMA) {
   setliftpress(3,8);
}
if (keycode == KeyEvent.VK_PERIOD) {
   setliftpress(3,9);
}
    }

    public void setliftpress(int liftid,int flowerid)
    {
         int liftnum =this.liftbean.length;
         if(liftid <liftnum)
         {
             LiftBean   lift = this.liftbean[liftid];
             int flowernum = lift.flowers.length;
             if(flowerid  <=flowernum)
             {
                 LiftFlower  flower = lift.flowers[flowerid-1];
                 flower.setIspress(true);
                 if(!lift.isIsrun())
                 {
                     MainControl liftcontrol = new MainControl(this, lift);
                     liftcontrol.start();
                 }
             }

         }

    }

    public void closeDoor()
    {
         LiftBean  lift =liftbean[0];
        for(int i=0;i<lift.getDx()/2;i++)
        {
            lift.setXx(i);
            try {
                Thread.sleep(5);
            } catch (InterruptedException ex) {
            }
            this.repaint();
        }
    }

    public void openDoor()
    {
        LiftBean lift = liftbean[0];
        for (int i = 0; i < lift.getDx() / 2; i++) {
            lift.setXx(lift.getDx() / 2 -i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
            }
            this.repaint();
        }

    }

    public LiftBean[] getLiftbean() {
        return liftbean;
    }

    public FlowerControl getFlowercontrol() {
        return flowercontrol;
    }

    //    public LiftFlower[] getLiftflower() {
//        return liftflower;
//    }

    public void setLiftbean(LiftBean[] liftbean) {
        this.liftbean = liftbean;
    }

    public void setFlowercontrol(FlowerControl flowercontrol) {
        this.flowercontrol = flowercontrol;
    }

    //    public void setLiftflower(LiftFlower[] liftflower) {
//        this.liftflower = liftflower;
//    }

    public RudyEllipse2D find(Point2D p) {
        for (int i = 0; i < controllist.size(); i++) {
            RudyEllipse2D r = (RudyEllipse2D) controllist.get(i);
            if (r.contains(p))
                return r;
        }
   return null;
}


    private class MouseHandler extends MouseAdapter
  {
     public void mousePressed(MouseEvent event)
     {
        // add a new square if the cursor isn't inside a square
    //    current = find(event.getPoint());
//        if (current == null)
//        add(event.getPoint());
     }

     public void mouseClicked(MouseEvent event)
     {
        // remove the current square if double clicked
        current = find(event.getPoint());
//        if(current.isIspress())
//        {
//            current.setIspress(false);
//        }else
//        {
//             current.setIspress(true);
//        }
        if(current != null)
        {
            setControlbutton(current);
            //    maincontrol = new MainControl(panel);
            //    maincontrol.start();
            repaint();
        }
//        MainControl   mainControl = new MainControl(panel);
//        mainControl.start();

//        if (current != null && event.getClickCount() >= 2)
//           remove(current);
     }

//     public void setPanel(MainPanel  panel)
//     {
//         this.panel = panel;
//     }
//     MainPanel  panel;
  }

  private class MouseMotionHandler
     implements MouseMotionListener
  {
     public void mouseMoved(MouseEvent event)
     {
        // set the mouse cursor to cross hairs if it is inside
        // a rectangle

        if (find(event.getPoint()) == null)
           setCursor(Cursor.getDefaultCursor());
        else
           setCursor(Cursor.getPredefinedCursor
              (Cursor.CROSSHAIR_CURSOR));
     }

     public void mouseDragged(MouseEvent event)
     {
//        if (current != null)
//        {
//           int x = event.getX();
//           int y = event.getY();
//
//           // drag the current rectangle to center it at (x, y)
//           current.setFrame(
//              x - SIDELENGTH / 2,
//              y - SIDELENGTH / 2,
//              SIDELENGTH,
//              SIDELENGTH);
//           repaint();
//        }
     }
  }

  public int chooseliftid =-1;



}
